<!--
 * @Descripttion: 描述
 * @Author: 刘黎
 * @Date: 2024-06-27 19:40:48
 * @LastEditors: 刘黎
 * @LastEditTime: 2024-08-02 17:14:20
-->
<!-- 新增好友 -->
<template>
  <div class="apption">
    <!-- 标题 -->
    <div class="pageTitle">公告管理</div>
    <div class="topBox">
      <div class="searchTop">
        <div class="left">
          <!-- <el-form :inline="true" :model="prop" class="demo-form-inline" label-width="100px">
            <el-form-item label="经销商:">
              <el-input v-model="prop.name" placeholder="请输入经销商名称" />
            </el-form-item>
            <el-form-item label="联系电话:">
              <el-input v-model="prop.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-form> -->
        </div>
        <div class="right">
          <el-button type="primary" @click="init">查询</el-button>
          <el-button type="success" @click="resetClick">重置</el-button>
          <el-button type="primary" @click="addClick">新增</el-button>
        </div>
      </div>
    </div>
    <div class="tableClass">
      <el-table :data="tableData" v-loading="loading" border style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="content" label="公告内容" />
        <el-table-column label="状态">
          <template #default="scope">
            <template v-if="scope.row.status==1">启用</template>
            <template v-if="scope.row.status==2">停用</template>
          </template>
        </el-table-column>
        <el-table-column label="创建时间 ">
          <template #default="scope">
            {{ scope.row.create_time }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button type="danger" size="small" @click="deleteClick(scope.row)">删除</el-button>
            <el-button type="primary" size="small" @click="editClick(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="timedPagination">
      <el-pagination
        v-model:current-page="prop.page"
        v-model:page-size="prop.pageSize"
        :total="prop.total"
        background
        layout="prev, pager, next"
        @current-change="handleCurrentChange">
      </el-pagination>
    </div>
    <!-- 新增or编辑分类 -->
    <el-dialog
      v-model="dialogVisible"
      width="600"
      :before-close="dialogClose"
      title="添加公告"
      top="3vh"
    >
      <div class="dialogContent">
        <el-form :model="addPropForm" label-width="auto">
          <el-form-item label="公告内容:">
            <el-input v-model="addPropForm.content" placeholder="请输入内容名称" />
          </el-form-item>
          <el-form-item label="公告状态:">
            <el-select v-model="addPropForm.status" clearable placeholder="请选择">
              <el-option
                label="启用"
                value="1"/>
              <el-option
                label="停用"
                value="2"/>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="loading" type="primary" @click="saveFun()">保存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>


<script setup>
import { computed,ref,onMounted } from 'vue'
import { useChatInfoStore, useJobIDStore } from '@/store/index.js'
import { ElMessageBox, ElMessage } from 'element-plus'
import { postApi } from "@/api/api.js"
import elUploadOptionMultiple from '@/components/elUploadOptionMultiple.vue';

let prop = ref({
  status: '',
  title: '',
  create_time: '',
  create_admin: '',
  page: 1,
  limit: 10,
  total: 0
})
let loading = ref(false)
let tableData = ref([])

// 查询商品
function init() {
  loading.value = true
  let obj = {
    page: prop.value.page,
    limit: prop.value.limit
  }
  postApi('/agent.notice/index', obj).then(res => {
    loading.value = false
    tableData.value = res.data
    prop.value.total = res.count
  }).catch(err => {
    loading.value = false
  })
}
function resetClick() { // 重置
  prop.value = {
    page: 1,
    limit: 10,
    total: 0
  }
  init()
}
function handleCurrentChange(val){
  prop.value.page = val
  init()
}

// 新增商品
let dialogVisible = ref(false)
let addPropForm = ref({
  id: '',
  content: '',
  status: '' // 状态(1:启用,2:禁用)
})

function addClick() {
  dialogVisible.value = true
  addPropForm.value = {
    id: '',
    content: '',
    status: '' // 状态(1:启用,2:禁用)
  }
}
function editClick(row) {
  dialogVisible.value = true
  addPropForm.value = {
    id: row.id,
    content: row.content,
    status: row.status + ''
  }
}
function deleteClick(row) {
  ElMessageBox.confirm('确定要删除该条数据吗？','删除').then(() => {
    postApi('/agent.notice/delete', { id:row.id }).then(res => {
      if(res.code === 1){
        ElMessage({
          message: "操作成功",
          type: 'success'
        })
        init()
      }
    })
  })
}
// 保存
function saveFun() {
  loading.value = true
  if(addPropForm.value.id){
    let obj = {
      id: addPropForm.value.id,
      content: addPropForm.value.content,
      status: addPropForm.value.status
    }
    postApi('/agent.notice/edit', obj).then(res => {
      if(res.code === 1){
        ElMessage({message: "操作成功",type: 'success'})
        init()
        dialogVisible.value = false
      }
    }).catch(err => {
      loading.value = false
    })
  }else{
    let obj = {
      content: addPropForm.value.content,
      status: addPropForm.value.status
    }
    postApi('/agent.notice/add', obj).then(res => {
      if(res.code === 1){
        ElMessage({message: "操作成功",type: 'success'})
        init()
        dialogVisible.value = false
      }
    }).catch(err => {
      loading.value = false
    })
  }
}
// 关闭弹框
function dialogClose(params) {
  dialogVisible.value = false
}

onMounted(()=>{
  init()
})
</script>

<style lang="scss" scoped>
.apption{
  padding: 20px;
  height: 100vh;
  .topBox{
    position: relative;
    background-color: #fff;
    border-radius: 12px;
    padding: 20px 20px 10px 20px;
    margin: 20px 0;
    .searchTop{
      display: flex;
      justify-content: space-between;
      .left{
        width: calc(100% - 260px);
        display: flex;
        align-items: center;
        flex-wrap: wrap;
      }
      .right{
        width: 260px;
      }
    }
  }
  .pageTitle{
    margin-bottom: 10px;
    font-weight: 700;
    font-size: 16px;
    color: rgb(16, 16, 16);
  }
}
</style>
